<?php
		
	/* SVN FILE: $Id: security.php 8 2011-03-13 08:32:38Z michele.andreoletti@gmail.com $ */
	
	/**
	 * Project Name : arcadia
	 *
	 * @author $Author: michele.andreoletti@gmail.com $
	 * @version $Revision: 8 $
	 * @lastrevision $Date: 2011-03-13 08:32:38 +0000 (Sun, 13 Mar 2011) $
	 * @filesource $URL: http://arcadia.googlecode.com/svn/trunk/include/security.php $
	 */

	if (!isset($sAPP) || !is_string($sAPP) || !isset($aSUITE) || !is_array($aSUITE) || !isset($aSUITE[$sAPP])) {

	  /**
		 * Intercetto la situazione in cui viene eseguito direttamente questo script (ovvero quando
		 * questo script non e' incluso in $sAPP/security.php
		 *
		 */

		header('Location: ../index.php?sMsg=app-not-defined');
		die();
	} 

	fnGetValueFromGET(array('sAction','sWhat','iJustAdded'));
	
	$sScript = fnCurrentScript();
	$sSCRIPT_ROOT_PATH = (fnIsScriptInRoot() ? '.' : '..'); // Variabile utilizzata in template.php
	
	$iAccessLvl = fnGetAccessLvl('app');

	$bSEARCH_FORM_SHOW = false;
	$sJQueryValidateRule = '';

	$sMessageFlash = fnMessageFlash();

	$sLEFT_NAV_IMG = "lock.png";
	$sHeaderTitle = $sTableTitle = "$sAPP_NAME &raquo; Gestione Accessi";

	// Le seguenti righe mi servono per avere un array dei valori di sTab associati a ciascun sViewItemLink ** INIZIO **
	$aSelectOptions = array();
	foreach ($aVIEW as $sViewItemLink => $aValues) {
		foreach ($aValues['aTabsLabels'] as $sIndex => $sValue) {
			$aSelectOptions[$sViewItemLink][] = array('iOptionValue' => $sIndex, 'sOptionDisplay' => $sIndex.". ".substr($sValue,0,strpos($sValue, '|')));
		}
	}
	// Le seguenti righe mi servono per avere un array dei valori di sTab associati a ciascun sViewItemLink ** CONTINUA... **
							
	if (is_allowed('sysop', $iAccessLvl)) {
	
		switch ($sAction) {

			case 'new':

				//
				// CODICE HTML PAGINA "Gestione Accessi » Aggiungi"
				//
				
				$sDocumentReady = '';
				$aAccessLvlValues = array ('-1', '1', '2', '3');
		
				$sHeaderTitle .= " &raquo; Aggiungi";
				$sTableTitle .= " &raquo; Aggiungi";

				if ($sWhat == 'sApp') {
					$sTitle = 'Applicazione';
					$aFields = array();
					$aAccessLvlValues[] = '9'; // iAccessLvl == 9 (sysop) è utilizzabile solo quando $sWhat == 'sApp'
			
				} else if ($sWhat == 'sTable') {
					$sTitle = 'Tabella';
					$aFields = array('sTable'=>'Tabella');
					
				} else if ($sWhat == 'sViewItemLink') {
					$sTitle = 'Pagina';
					$aFields = array('sViewItemLink'=>'Pagina');	
					
				} else if ($sWhat == 'sTab') {
					$sTitle = 'Scheda';
					$aFields = array('sViewItemLink'=>'Pagina', 'sTab'=>'Scheda');	
					
				} else if ($sWhat == 'sField') {
					$sTitle = 'Campo';
					$aFields = array('sViewItemLink'=>'Pagina', 'sTab'=>'Scheda', 'sField'=>'Campo');		
				
				}
				
				$html .= "<a href='./security.php'>&laquo; Annulla</a>";

				$html .= "<div>&nbsp;</div>";
		
				$html .= "<form id='security-new' method='get' action='./security.php'>";

				$html .= "<input type='hidden' name='sAction' value='add'>";
			
				$html .= "<table>
							<col width='40%'><col width='60%'>
							<tbody>";
				
				// sType
				$html .= "	<tr>
								<th>&nbsp;</th>
								<td><select name='sType'>
											<option value='user'>user</option>
											<option value='group'>group</option>
										</select></td>
							</tr>";
				
				// sUsername
				$html .= "	<tr>
								<th>Utente/Gruppo</th>
								<td><input type='text' name='sUsername'></td>
							</tr>";
				
				foreach ($aFields as $sField => $sLabel) {
					if ($sField == 'sTable') {
						// sTable
						$html .= "	<tr>
										<th>$sLabel</th>
										<td><select name='$sField'>";
						$html .= "			<option value=''>Selezione $sLabel...</option>";
						foreach ($aTABLE as $sTable => $aValues) {
							if ($aValues['iShow'] == 1)	
							$html .= "		<option value='$sTable'>".$aTABLE[$sTable]['sTableName']."</option>";
						}
						$html .= "	</select></td>
									</tr>";
					
					} else if ($sField == 'sViewItemLink') {
						// sViewItemLink
						$html .= "	<tr>
										<th>$sLabel</th>
										<td><select name='$sField'>";
						$html .= "			<option value=''>Selezione $sLabel...</option>";
						foreach ($aVIEW as $sViewItemLink => $aValues)
							$html .= "		<option value='$sViewItemLink'>".$sViewItemLink."</option>";
						$html .= "	</select></td>
									</tr>";
					
					} else if ($sField == 'sTab') {
						// sTab
						$html .= "	<tr>
										<th>$sLabel</th>
										<td><select name='$sField'>";
						$html .= "			<option value=''>Selezione $sLabel...</option>";
						$html .= "	</select></td>
									</tr>";
						
						
						$sDocumentReady .= 	"
							// Le seguenti righe mi servono per avere un array dei valori di sTab associati a ciascun sViewItemLink ** ...CONTINUA 1/2 **
							$(\"select[name='sViewItemLink']\").change( function() {
								var aSelectOptions = ".json_encode($aSelectOptions)."
								var options = '';
								for (var i = 0; i < aSelectOptions[$(this).val()].length; i++) {
									options += \"<option value='\" + aSelectOptions[$(this).val()][i].iOptionValue + \"'>\" + aSelectOptions[$(this).val()][i].sOptionDisplay + \"</option>\"; }
								$(\"select[name='sTab']\").html(options);

							});
							// Le seguenti righe mi servono per avere un array dei valori di sTab associati a ciascun sViewItemLink ** FINE 1/2 ** ";
						
								 
					} else {
						$html .= "	<tr>
										<th>$sLabel</th>
										<td><input type='text' name='$sField'></td>
									</tr>";
				
					}
				}
	
				$html .= "	<tr>
								<th>Accesso a $sTitle</th>
								<td><select name='iLvl'>";
				$html .= "		<option value=''>Seleziona Accesso...</option>";
				foreach ($aAccessLvlValues as $iLvl) {
					$html .= "	<option value='$iLvl'>".fnDisplayAccessLvl($iLvl,'text')."</option>";
				}
				$html .= "	</select></td>
							</tr>";

				$html .= "	<tr>
								<th>&nbsp;</th>
								<td><input type='hidden' name='sWhat' value='$sWhat'>
									<input type='hidden' name='sAPP' value='$sAPP'></td>
							</tr>";
				
				$html .= "	<tr>
								<th>&nbsp;</th>
								<td><button id='submit' type='submit'>Salva</button></td>
							</tr>";
			
				$html .= "</tbody></table>";
				$html .= "</form>";


				$sHeadContent = "
					<script type='text/javascript' src='../sysop/include/user.js'></script>
		
					<script type='text/javascript' src='../include/jquery.validate.min.js'></script>
					<script type='text/javascript' src='../include/messages_it.js'></script>
					<script type='text/javascript' src='../include/methods_it.js'></script>
					<script type='text/javascript'>
						
						$().ready(function() {
							
							$sDocumentReady
							
							$('button').button();

							$(\"input[name='sField']\").autocomplete({
									source: './security.autocomplete.php',
									minLength: 2,
									select: function(event, ui) {
										$(this).val(ui.item.value);
									}
								});

							$(\"input[name='sUsername']\").autocomplete({
								source: '../include/username.autocomplete.php',
								select: function(event, ui) {
									$(this).val(ui.item.value);
								}
							}); ";

				// jQuery plugin: Validation ** INIZIO **
				$sHeadContent .= 	"
							$('#security-new').validate({
								rules: {
									sUsername: { required: true },
									iLvl: { required: true }
									";
								if (!empty($aFields)) {
									// Tutti i campi del form sono obbligatori
									foreach ($aFields as $sField => $sLabel) {
										$sHeadContent .= ", $sField: { required: true } ";
									}
								}
				$sHeadContent .= 	"
								}
							}); ";
				// jQuery plugin: Validation ** FINE **

				$sHeadContent .= 	"
						});
					</script> ";
	
			break;



			case 'add':
			
				//
				// CODICE MYSQL: INSERT INTO `settings` ...
				//
				
				fnGetValueFromGET(array('sUsername','sType','iLvl','sTable','sViewItemLink','sTab','sField'));

				$sQuery = "	INSERT INTO `users_access_lvl`
								(`id`, `sUsername`, `sType`, `sApp`, `sTable`, `sViewItemLink`, `sTab`, `sField`, `sWhat`, `iAccessLvl`)
										VALUES
								(NULL, '$sUsername', '$sType', '$sAPP', '$sTable', '$sViewItemLink', '$sTab', '$sField', '".strtolower(substr($sWhat,1))."', '$iLvl')";
																																												// strtolower(substr($sWhat,1)) ==> elimino la 's' iniziale
																																												// e trasformo tutte le lettere in minuscolo (sTab diventa tab,
																																												// sViewItemLink diventa viewitemlink, ...
				$rResult = custom_mysql_query($sQuery);	
				$iJustAdded = mysql_insert_id();
				
				header("Location: ./security.php?iJustAdded=$iJustAdded#$sWhat");
				die();
				
			break;



			default:
		
				// inizializzo l'array nel caso in cui, dopo le prossime istruzioni, rimanga completamente vuoto
				$aAccessLvl	= array(); 
				
				$sQuery = "SELECT * FROM `users_access_lvl` WHERE `sApp`='$sAPP' ORDER BY `sTable`, `sViewItemLink`, `sTab`, `sField`, `sUsername`";
				$rResult = custom_mysql_query($sQuery);
				
				if (mysql_num_rows($rResult) > 0) {
					
					while ($aRow = mysql_fetch_array($rResult)) {
						
						if ($aRow['sWhat'] == 'app') {
							$aAccessLvl	['sApp'][] = array('id' => $aRow['id'], 'sUsername' => $aRow['sUsername'], 'sType' => $aRow['sType'], 'iAccessLvl' => $aRow['iAccessLvl']);
						
						} else if ($aRow['sWhat'] == 'table') {
							$aAccessLvl	['sTable'][] = array('id' => $aRow['id'], 'sUsername' => $aRow['sUsername'], 'sType' => $aRow['sType'], 'sTable' => $aRow['sTable'], 'iAccessLvl' => $aRow['iAccessLvl']);
						
						} else if ($aRow['sWhat'] == 'viewitemlink') {
							$aAccessLvl	['sViewItemLink'][] = array('id' => $aRow['id'], 'sUsername' => $aRow['sUsername'], 'sType' => $aRow['sType'], 'sViewItemLink' => $aRow['sViewItemLink'], 'iAccessLvl' => $aRow['iAccessLvl']);
					
						} else if ($aRow['sWhat'] == 'tab') {
							$aAccessLvl	['sTab'][] = array('id' => $aRow['id'], 'sUsername' => $aRow['sUsername'], 'sType' => $aRow['sType'], 'sViewItemLink' => $aRow['sViewItemLink'], 'sTab' => $aRow['sTab'], 'iAccessLvl' => $aRow['iAccessLvl']);
					
						} else if ($aRow['sWhat'] == 'field') {
							$aAccessLvl	['sField'][] = array('id' => $aRow['id'], 'sUsername' => $aRow['sUsername'], 'sType' => $aRow['sType'], 'sViewItemLink' => $aRow['sViewItemLink'], 'sTab' => $aRow['sTab'], 'sField' => $aRow['sField'], 'iAccessLvl' => $aRow['iAccessLvl']);
						
						}
						
					}	
				
					$html .= fnHtmlAccessLvlTable('sApp', $aAccessLvl);
					$html .= fnHtmlAccessLvlTable('sTable', $aAccessLvl);
					$html .= fnHtmlAccessLvlTable('sViewItemLink', $aAccessLvl);
					$html .= fnHtmlAccessLvlTable('sTab', $aAccessLvl);
					$html .= fnHtmlAccessLvlTable('sField', $aAccessLvl);
					
					$html .= "
					<div id='trash-dialog' title='Elimina'>
						<p>Sei sicuro di voler eliminare la voce selezionata?</p>
						<p>Questa operazione non potrà essere annullata.</p>
					</div>";
				
					$sHeadContent = "
						<script type='text/javascript' src='".$sSCRIPT_ROOT_PATH."/sysop/include/user.js'></script>
										
						<link type='text/css' href='".$sSCRIPT_ROOT_PATH."/include/dataTables/media/css/demo_table.css' rel='stylesheet' />
				
						<script type='text/javascript' charset='utf-8'>
						
							function fnRemoveSelected () {
								$('.security-table').children('tbody').children('tr').each(function() {
									$(this).removeClass('row_selected');
								});
							}
					
							$(document).ready(function() {
								
								". 	/* 	se è presente il parametro iJustAdded (./security.php?iJustAdded=81)
										allora evidenzio con la funzione fnHighLight la riga appena inserita	*/
									
									(isset($iJustAdded) && !empty($iJustAdded) ? " fnHighLight ($('tr[iUsersAccessLvl=$iJustAdded]')) " : '')."

								$('.edit, .indicator').hide();
		
								$(\"input[name='sField']\").autocomplete({
									source: './security.autocomplete.php',
									minLength: 2,
									select: function(event, ui) {
										$(this).val(ui.item.value);
									}
								});
								
								$(\"input[name='sUsername']\").autocomplete({
									source: '../include/username.autocomplete.php',
									select: function(event, ui) {
										$(this).val(ui.item.value);
									}
								});
								
								// Le seguenti righe mi servono per avere un array dei valori di sTab associati a ciascun sViewItemLink ** ...CONTINUA 2/2 **
								$(\"select[name='sViewItemLink']\").change( function() {
									var aSelectOptions = ".json_encode($aSelectOptions)."
									var options = '';
									for (var i = 0; i < aSelectOptions[$(this).val()].length; i++) {
										options += \"<option value='\" + aSelectOptions[$(this).val()][i].iOptionValue + \"'>\" + aSelectOptions[$(this).val()][i].sOptionDisplay + \"</option>\"; }
									$(this).parents().next().children('select').html(options);
								});
								// Le seguenti righe mi servono per avere un array dei valori di sTab associati a ciascun sViewItemLink ** FINE 2/2 **
						
								function HoverImgOf(filename) {
								   var re = new RegExp('(.+)\\.(gif|png|jpg)', 'g');
								   return filename.replace(re, '$1_f2.$2'); }
								function NormalImgOf(filename) {
								   var re = new RegExp('(.+)_f2\\.(gif|png|jpg)', 'g');
								   return filename.replace(re, '$1.$2'); }
								
								$('.edit-trigger, .cancel-trigger, .save-trigger, .trash-trigger').hover( function() {
										var hoverImg = HoverImgOf($(this).attr('src'));
										$(this).attr('src', hoverImg);
									}, function() {
										var normalImg = NormalImgOf($(this).attr('src'));
										$(this).attr('src', normalImg);
									}
								);
		
								$('.edit-trigger').click(function() {
									$('.edit-trigger').hide();
									$('.trash-trigger').hide();
									$(this).parents('tr:first').prev('tr').show();
									$(this).parents('tr:first').hide();
								});
								
								$('.cancel-trigger').click(function() {
									
									// annullo le modifiche copiando i valori di ogni campo dalla riga
									// successiva (view) alla riga corrente (edit)
									$(this).parents('tr:first').next().children().each(function(index) {
										if ($(this).attr('sField') != undefined && $(this).attr('sValue') != undefined)
											$(this).parents('tr:first').prev().children().children(\"input[name='\" + $(this).attr('sField')+\"'], select[name='\"+$(this).attr('sField')+\"']\").val($(this).attr('sValue'));
									});
									
									$('.edit-trigger').show(); // visualizzo l'icona 'edit'
									$('.trash-trigger').show(); // visualizzo l'icona 'trash'
									$(this).parents('tr:first').next('tr').show(); // visualizzo la riga 'view'
									$(this).parents('tr:first').hide(); // nascondo la riga 'edit'
									
								});
								
								$('.trash-trigger').click(function() {
									fnRemoveSelected();
									oClicked = $(this);
									oClicked.parents('tr:first').addClass('row_selected');
									$('#trash-dialog').dialog('open');
									return false;
								});
								
								$('#trash-dialog').dialog({
									autoOpen: false,
									resizable: false,
									modal: true,
									buttons: {
										'Elimina': function() {
											$.getJSON('../include/security.json.php', { 'sAPP': '$sAPP', 'id': oClicked.attr('iUsersAccessLvl'), 'sAction': 'trash' },  function (json) {
												if (json[0].sScript=='security.json.php' && json[0].sAction=='trash' && json[0].sError=='') {
													// salvo l'oggetto <tbody> relativo alle righe che voglio cancellare
													oTbody = $('tr[iUsersAccessLvl='+oClicked.attr('iUsersAccessLvl')+']').parent('tbody:first');
													// elimino le righe colorate (gli stili 'even' e 'odd')
													oTbody.children('tr').removeClass('even odd');
													$('tr[iUsersAccessLvl='+oClicked.attr('iUsersAccessLvl')+']').prev().remove(); // cancello la riga 'edit'
													$('tr[iUsersAccessLvl='+oClicked.attr('iUsersAccessLvl')+']').remove(); // cancello la riga 'view'
													// riassegno le classi 'even' e 'odd'
													oTbody.children('tr.view:even').addClass('odd');
													oTbody.children('tr.view:odd').addClass('even');
													
												} else {
													alert ('Error: ' + json[0].sError);
												
												}
											});
											// chiudo la finestra di dialogo
											$(this).dialog('close');
										},
										Annulla: function() {
											fnRemoveSelected();
											$(this).dialog('close');
										}
									},
									close: function() {
										fnRemoveSelected();
									}
								});
								
								$('.save-trigger').click(function() {
									
									$('.indicator').show();
									$('.cancel-trigger').hide();
									
									var aData = {};
									aData['sAPP'] = '$sAPP';
									aData['sAction'] = 'edit';
									
									// invio il form leggendo i valori di ogni campo dalla riga corrente (edit)
									
									$(this).parents('tr:first').children().each(function(index) {
										if ($(this).children('input, select').val() != undefined)
											aData[$(this).children('input, select').attr('name')] = $(this).children('input, select').val();
									});
									
									$.getJSON('../include/security.json.php', aData, function (json) {
										if (json[0].sScript=='security.json.php'  && json[0].sAction=='edit' && json[0].sError=='') {
											
											$('tr[iUsersAccessLvl='+json[0].id+']').children().each(function(index) {
												
												// Verifico che l'elemento <TD> abbia un attributo di nome 'sField'.
												// Gli elementi esclusi (senza attributo 'sField' sono quelli riferiti
												// alle immagini con classe '.edit', '.cancel', '.trash'
												
												if ($(this).attr('sField') != undefined) {
													// sostituisco completamente l'elemento <TD>
													$(this).replaceWith(json[0][$(this).attr('sField')]);
												}
												
											});
											
											fnHighLight ($('tr[iUsersAccessLvl='+json[0].id+']'));
											
										} else {
											alert ('Error: ' + json[0].sError);
										}
					
										$('.indicator').hide();
										$('.cancel-trigger').show();
										
										$('.edit-trigger').show(); // visualizzo l'icona 'edit'
										$('.trash-trigger').show(); // visualizzo l'icona 'trash'
										
										$('tr.view').show(); // visualizzo le righe 'view'
										$('tr.edit').hide(); // nascondo la righe 'edit'
									
									});
								
								});		
								
							});
		
						</script> ";
					
				break;
			

			}
		}
		
	} else {
		fnUpdateLog('LOW RIGHTS', 0, '','S');
		header("Location: ./index.php?sMsg=low-rights");
		die();

	}		
	
	require_once('../themes/'.$sTHEME.'/template.php');
	die();
	
?><html><body bgcolor="#fff"></body></html>





<?php

	function fnHtmlAccessLvlTable ($sWhat, $aAccessLvl) {

		global $aTABLE;		
		global $sTABLE_DEFAULT;
		global $aVIEW;
		
		$aAccessLvlValues = array ('-1', '1', '2', '3');

		if ($sWhat == 'sApp') {
			$sTitle = 'Applicazione';
			$aColumns = array('sType'=>'&nbsp;', 'sUsername'=>'Utente/Gruppo', 'iAccessLvl'=>'Accesso');
			$aAccessLvlValues[] = '9'; // iAccessLvl == 9 (sysop) è utilizzabile solo per sApp
		
		} else if ($sWhat == 'sTable') {
			$sTitle = 'Tabella';
			$aColumns = array('sType'=>'&nbsp;', 'sUsername'=>'Utente/Gruppo', 'sTable'=>'Tabella', 'iAccessLvl'=>'Accesso');
		
		} else if ($sWhat == 'sViewItemLink') {
			$sTitle = 'Pagina';
			$aColumns = array('sType'=>'&nbsp;', 'sUsername'=>'Utente/Gruppo', 'sViewItemLink'=>'Pagina', 'iAccessLvl'=>'Accesso');
		
		} else if ($sWhat == 'sTab') {
			$sTitle = 'Scheda';
			$aColumns = array('sType'=>'&nbsp;', 'sUsername'=>'Utente/Gruppo', 'sViewItemLink'=>'Pagina', 'sTab'=>'Scheda', 'iAccessLvl'=>'Accesso');
		
		} else if ($sWhat == 'sField') {
			$sTitle = 'Campo';
			$aColumns = array('sType'=>'&nbsp;', 'sUsername'=>'Utente/Gruppo', 'sViewItemLink'=>'Pagina', 'sTab'=>'Scheda', 'sField'=>'Campo', 'iAccessLvl'=>'Accesso');
		
		}
		
		$html = "<div class='security-table-title'><a name='$sWhat'>$sTitle</a></div>";
		
		$html .= "<a href='./security.php?sAction=new&sWhat=$sWhat' class='security-add'>Aggiungi &raquo;</a>";
		
		$html .= "<table class='security-table display' cellspacing='0' cellpadding='0'>";
		
		$html .= "	<thead>
						<tr>";
		$html .= "			<th>&nbsp;</th>";
		foreach ($aColumns as $sField => $sLabel)
			$html .= "		<th>".$sLabel."</th>";
		$html .= "			<th>&nbsp;</th>";
		$html .= "		</tr>
					</thead>";
		
		$html .= "	<tbody>";
		
		if (isset($aAccessLvl[$sWhat])) {
			
			// creo un array con tutti gli sUsername definiti
			$aUsers = fnGetDataFromQuery("SELECT `sUsername`, `sType`, `iEnabled` FROM `users`");
			
			foreach ($aAccessLvl[$sWhat] as $aRow) {
			
				// EDIT
				$html .= "	<tr class='edit'>";
				$html .= "		<td style='width:40px;'><input type='hidden' name='id' value='".$aRow['id']."' />
									<img sWhat='$sWhat' title='salva' class='save-trigger' src='../themes/common/images/icn_save.png'>
									<img class='indicator' src='../themes/common/images/indicator.gif'>
									<img title='annulla' class='cancel-trigger' src='../themes/common/images/icn_cancel.png'></td>";
				
				foreach ($aColumns as $sField => $sLabel) {
					
					if ($sField == 'sType') {
						$html .= "<td><select name='$sField'>";
						$html .= "	<option value='user' ".($aRow[$sField]=='user' ? "selected='selected'" : '').">user</option>";
						$html .= "	<option value='group' ".($aRow[$sField]=='group' ? "selected='selected'" : '').">group</option>";
						$html .= "</select></td>";
					
					} else if ($sField == 'sTable') {
						$html .= "<td><select name='$sField'>";
						foreach ($aTABLE as $sTable => $aValues) {
							if ($aValues['iShow'] == 1)	
							$html .= "	<option value='$sTable' ".($aRow[$sField]==$sTable ? "selected='selected'" : '').">".$aTABLE[$sTable]['sTableName']."</option>";
						}
						$html .= "</select></td>";
					
					} else if ($sField == 'sViewItemLink') {
						$html .= "<td><select name='$sField'>";
						foreach ($aVIEW as $sViewItemLink => $aValues)
							$html .= "	<option value='$sViewItemLink' ".($aRow[$sField]==$sViewItemLink ? "selected='selected'" : '').">".$sViewItemLink."</option>";
						$html .= "</select></td>";
					
					} else if ($sField == 'sTab') {
						$html .= "<td><select name='$sField'>";
						if (isset($aVIEW[$aRow['sViewItemLink']]) && isset($aVIEW[$aRow['sViewItemLink']]['aTabsLabels'])) {
							foreach ($aVIEW[$aRow['sViewItemLink']]['aTabsLabels'] as $iTab => $sLabel)
								$html .= "	<option value='$iTab' ".($aRow[$sField]==$iTab ? "selected='selected'" : '').">".$iTab.". ".substr($sLabel,0,strrpos($sLabel,'|'))."</option>";
						} else {
							foreach ($aVIEW[$aTABLE[$sTABLE_DEFAULT]['sViewItemLink']]['aTabsLabels'] as $iTab => $sLabel)
								$html .= "	<option value='$iTab' ".($aRow[$sField]==$iTab ? "selected='selected'" : '').">".$iTab.". ".substr($sLabel,0,strrpos($sLabel,'|'))."</option>";
						}
						$html .= "</select></td>";
					
					} else if ($sField == 'iAccessLvl') {
						$html .= "<td><select name='$sField'>";
						foreach ($aAccessLvlValues as $iLvl) {
							$html .= "	<option value='$iLvl' ".($aRow[$sField]==$iLvl ? "selected='selected'" : '').">".fnDisplayAccessLvl($iLvl,'text')."</option>";
						}
						$html .= "</select></td>";
					
					} else
						$html .= "	<td><input type='text' name='$sField' value='".$aRow[$sField]."' size='16'/></td>";
				
				}
				
				$html .= "		<td style='width:40px;'><input type='hidden' name='sWhat' value='".$sWhat."' />
									<img sWhat='$sWhat' title='salva' class='save-trigger' src='../themes/common/images/icn_save.png'>
									<img class='indicator' src='../themes/common/images/indicator.gif'>
									<img title='annulla' class='cancel-trigger' src='../themes/common/images/icn_cancel.png'></td>";
				$html .= "	</tr>";
				
				// VIEW
				$sClass = (!isset($sClass) || $sClass == 'even' ? 'odd' : 'even');
				$html .= "	<tr class='$sClass view' iUsersAccessLvl='".$aRow['id']."'>";
				$html .= "		<td style='width:40px;'><img title='modifica' class='$sClass edit-trigger' src='../themes/common/images/icn_pencil.gif'></td>";
				
				foreach ($aColumns as $sField => $sLabel) {
					
					if ($sField == 'sType')
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'>".$aRow[$sField]."</td>";
					
					else if ($sField == 'sUsername')
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'><span class='".fnCheckUsernameTypeDisplayClass($aRow[$sField], $aRow['sType'], $aUsers)."'>".$aRow[$sField]."</span></td>";
					
					else if ($sField == 'sTable')
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'>".(isset($aTABLE[$aRow[$sField]]['sTableName']) ? $aTABLE[$aRow[$sField]]['sTableName'] : "<span class='error'>errore!</span>")."</td>";
					
					else if ($sField == 'sViewItemLink')
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'>".(isset($aVIEW[$aRow[$sField]]) ? $aRow[$sField] : "<span class='error'>errore!</span>")."</td>";
					
					else if ($sField == 'sTab')
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'>".(isset($aVIEW[$aRow['sViewItemLink']]['aTabsLabels'][$aRow[$sField]]) ? $aRow[$sField].". ".substr($aVIEW[$aRow['sViewItemLink']]['aTabsLabels'][$aRow[$sField]],0,strrpos($aVIEW[$aRow['sViewItemLink']]['aTabsLabels'][$aRow[$sField]],'|')) : "<span class='error'>errore!</span>")."</td>";
					
					else if ($sField == 'iAccessLvl')
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'>".fnDisplayAccessLvl($aRow[$sField],'html')."</td>";
					
					else
						$html .= "	<td sField='$sField' sValue='".$aRow[$sField]."'>".$aRow[$sField]."</td>";
				
				}
				
				$html .= "		<td style='width:20px;'><img title='elimina' iUsersAccessLvl='".$aRow['id']."' class='trash-trigger' src='../themes/common/images/icn_trash.gif'></td>";
				$html .= "	</tr>";
					
			}
		}
		
		$html .= "	<tr  class='empty' ".(isset($aAccessLvl[$sWhat]) ? "style='display:none;' " : '')."><td colspan='".count($aColumns)."'>Tabella vuota</td></tr>";
		
		$html .= "	</tbody>";
		
		$html .= "</table>";
		
		return $html;
	}	
			
?>